package com.estronger.shopmanage.manager;


import com.estronger.shopmanage.base.MyApplication;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.soonfor.evaluate.tools.CommonUtils;

import org.json.JSONObject;

import java.io.File;

import cn.jesse.nativelogger.NLogger;

public abstract class NetManager {

    private static HttpUtils httpUtils = new HttpUtils();
    public static String getDownloadURL() {
        return MyApplication.getApplication().getSERVER_URL()
                + "/javascript/kindeditor/asp.net/appfile_download.ashx";
    }

    // 首页及登录接口
    public static final short LOGOUT = 0;
    public static final short LOGIN = 1;
    public static final short ADS = 2;
    public static final short FGET_PSWD = 3;
    public static final short EXIT = 4;
    public static final short CG_PSWD = 5;
    public static final short CG_DATA = 6;
    public static final short UP_DATA = 7;
    public static final short WEATHER = 8;
    public static final short SHARE = 9;
    public static final short UP_AVATAR = 10;
    // 测量接口
    public static final short MEASURE_LIST = 11;
    public static final short DESIGNERS = 12;
    public static final short OTHER = 13;
    public static final short GET_TASK = 14;
    public static final short CONTACT = 15;
    public static final short CONTENT = 16;
    public static final short RESULT = 17;
    public static final short DETAIL = 18;
    public static final short CONFIRM = 19;
    // 送货接口
    public static final short SEND_LIST = 21;
    public static final short SEND_GET_TASK = 22;
    public static final short SEND_CONTACT = 23;
    public static final short SEND_DETAIL = 24;
    public static final short SEND_CONTENT = 25;
    public static final short SEND6 = 26;
    public static final short SEND7 = 27;
    public static final short DSEND = 28;
    // 安装接口
    public static final short BUILD_LIST = 31;
    public static final short BUILD_GET_TASK = 32;
    public static final short BUILD_DETAIL = 33;
    public static final short BUILD4_CONTENT = 34;
    public static final short BUILD_RESULT = 35;
    public static final short BUILD_FINISH = 36;
    public static final short BUILD7 = 37;
    public static final short BUILD_ABN_TYPES = 38;

    // 个人中心接口
    public static final short CG_PHONE = 41;
    public static final short CG_MAIL = 42;
    public static final short CG_WEIXIN = 43;
    public static final short CG_QQ = 44;
    public static final short CG_NIKE = 45;
    // 各个任务数
    public static final short INFO = 46;
    public static final short TASK_SUM = 47;
    public static final short MEASURE_NUM = 48;
    public static final short SEND_NUM = 49;
    public static final short BUILD_NUM = 50;
    public static final short PG_MEASURE_NUM = 100;//测量派工
    public static final short ORDERQUERY_NUM = 65;//预订单查询
    public static final short AFTERMARKET_NUM = 22629;//售后标题数量
    public static final short AFTERMARKET_DETAIL = 22630;//售后任务详情
    public static final short AFTERMARKET_CONFIR_APP = 22704;//售后任务详情预约确认
    public static final short AFTERMARKET_CONFIR_COM = 22704;//售后任务详情完工确认
    /**
     * 预订单录入相关
     */
    public static final short ORDER_SELECT_DEFAULTSHOPPCD = 51;//获取门店默认的市区
    public static final short ORDER_SELECT_CITY = 52;//获取城市列表
    public static final short ORDER_SELECT_COUNTRY = 53;//获取区县列表
    public static final int ORDER_SELECT_HOUSES = 54;//获取楼盘列表
    public static final int ORDER_SELECT_ACTION = 55;//获取活动列表
    public static final int ORDER_SELECT_MEALTYPE = 56;//获取套餐类型列表
    public static final int ORDER_SELECT_SERIZES = 57;//获取系列列表
    public static final int ORDER_SELECT_STYLES = 58;//获取系列列表
    public static final int ORDER_SELECT_TEXTTURE = 59;//获取材质列表
    public static final int ORDER_SELECT_COLOR = 60;//获取颜色列表
    public static final int ORDER_SELECT_GATHERING = 61;//获取颜色列表
    public static final int ORDER_SELECT_MEALTYPE_SHUX = 62;//获取套餐属性列表
    public static final int ORDER_UPLOAD_PIC = 63;//上传图片
    public static final int ORDER_ADDPREORD = 64;//新增预订单

    /**
     *预订单查询相关
     */
    public static final short ORDER_ORDERQUERY = 66;//获取预订单列表
    public static final short ORDER_ORDERDETAIL = 67;//获取预订单详情

    // 图片相关
    public static final short IMGUPDATE = 10000;
    public static final short IMGDOWNLOAD = 10001;
    public static final short FILEUPDATE = 10002;

    //送货完工登记选择图品
    public static final short SIGINING_CHOOSE = 300;//签收图片
    public static final short ABNORMAL_CHOOSE = 400;//异常图片 abnormal
    //安装完工登记选择图片
    public static final short BEFORE_CHOOSE = 500;
    public static final short BUILD_ING_CHOOSE = 600;
    public static final short AFTER_CHOOSE = 700;

    // 测量派工接口
    public static final short PG_PERMISSION = 99;//待派工列表
    public static final short PG_MEASURE_LIST = 101;//待派工列表
    public static final short PG_MS_DETAIL = 102;//任务详情
    public static final short PG_MS_CONDTION_SURE = 103;//条件确认
    public static final short PG_MS_SAVE = 104;//请求派工接口
    public static final short PG_CHOISE_DESIGNER = 105;//选择设计师
    public static final short MEASURE_TRIP = 106;//设计师行程

    public static final short PG_CHOISE_INSTALLER = 107;//选择安装师

    //安装师行程
    public static final short INSTALL_TRIP = 112;
    //待安装任务数量
    public static final short INSTALL_DAINUM = 113;
    //请求待安装与已安装的清单
    public static final short INSTALL_PG = 114;
    //送货待派工的数量
    public static final short DELIVER_DAINUM = 115;
    //送货待派工和已派工
    public static final short DELIVER_PG = 116;
    //送货员行程
    public static final short DELIVER_TRIP = 117;
    //送货员列表
    public static final short DELIVER_MENBER = 118;
    //送货商品清单
    public static final short DELIVER_GOODSLIST = 119;
    //获取送货任务详情
    public static final short DELIVER_DLVINFO = 120;
    //提交送货派工信息
    public static final short DELIVER_PGSUBMIT = 121;
    //预订单新增选个年龄字段
    public static final int ORDER_SELECT_AGE = 122;
    public static final int ORDER_SELECT_CUSTOMRES = 123;//获取客户来源列表

    //批量派工
    public static final int GETLIST_PG_DATAS = 201;//获取待派工数据
    public static final int GETLIST_BATCHPG = 201;//批量获取待派工数据
    public static final int SAVE_BATCHPG = 202;



    public void setTimeOut(int second) {
        httpUtils.configTimeout(second * 1000).configSoTimeout(second * 1000).configRequestRetryCount(2);
    }

    public void connectWork(RequestParams params, final int code) {
        String url = "";
        if (code == IMGUPDATE || code == FILEUPDATE || code == BEFORE_CHOOSE
                || code == BUILD_ING_CHOOSE || code == AFTER_CHOOSE || code == ORDER_UPLOAD_PIC) {
            url = MyApplication.getApplication().getSERVER_URL()
                    + "/javascript/kindeditor/asp.net/appupload_json.ashx";
        } else if (code == IMGDOWNLOAD) {
            url = MyApplication.getApplication().getSERVER_URL()
                    + "/javascript/kindeditor/asp.net/appfile_download.ashx";
        } else {
            url = MyApplication.getApplication().getSERVER_URL()
                    + "/web/common/API/apphandler.ashx";
        }
        httpUtils.send(HttpMethod.POST, url, params,
                new RequestCallBack<String>() {

                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        String result = responseInfo.result;
                        if(result==null || result.equals("")){
                            onNetWorkErorr(code, "请求结果为空");
                        }else {
                            try {
                                onNetWorkSuccess(code, result);
                            }catch (Exception e1){
                                try {
                                    JSONObject jsonObject = new JSONObject(result);
                                    boolean isSuccess = jsonObject.getBoolean("success");
                                    if(isSuccess){
                                        onNetWorkSuccess(code, result);
                                    }else {
                                        String msgdata = CommonUtils.formatStr(jsonObject.getString("data"));
                                        if(msgdata.equals("")){
                                            String msg = CommonUtils.formatStr(jsonObject.getString("msg"));
                                            if(!msg.equals("")) onNetWorkErorr(code, msg);
                                        }else onNetWorkErorr(code, msgdata);
                                    }
                                }catch (Exception e2){}
                            }
                        }
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        NLogger.i("网络请求异常:" + msg);
                        if(msg.contains("Proxy Error")){
                            msg = "网络错误";
                        }else {
                            msg = "网络或服务器异常:" + msg;
                        }
                        onNetWorkErorr(code, msg);
                    }
                });
    }

    public void updateIMG(RequestParams params, final int code) {
        uploadFile(params,code,true);
    }

    public void uploadVideo(RequestParams params,int code){
        uploadFile(params,code,false);
    }

    public void uploadFile(RequestParams params,final int code,boolean isImage){
        String url = MyApplication.getApplication()
                .getSERVER_URL()
                + "/javascript/kindeditor/asp.net/appupload_json.ashx";
        if (isImage){
            url = url + "?dir=image&isvideoimg=0";
        }else {
            url = url + "?dir=video&isvideoimg=1";
        }
        httpUtils.send(HttpMethod.POST, url, params,
                new RequestCallBack<String>() {
                    @Override
                    public void onSuccess(ResponseInfo<String> responseInfo) {
                        onIMGSuccess(code, responseInfo.result);
                    }

                    @Override
                    public void onFailure(HttpException error, String msg) {
                        onNetWorkErorr(code, msg);
                    }
                });
    }

    public void get(String url, String target) {
        httpUtils.download(url, target, new RequestCallBack<File>() {
            @Override
            public void onSuccess(ResponseInfo<File> responseInfo) {
                onGetSuccess(responseInfo.result);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                onNetWorkErorr(1000, msg);
            }
        });
    }

    public void onGetSuccess(File result) {
    }

    public void onIMGSuccess(int code, String result) {
    }

    public abstract void onNetWorkSuccess(int code, String result);

    public abstract void onNetWorkErorr(int code, String result);


    public HttpUtils getHttpUtils() {
        return httpUtils;
    }

}
